/*
 * jQuery Progress Bar plugin
 * Version 2.0.0 (07/25/2008)
 * @requires jQuery v1.2.1 or later
 *
 * Copyright (c) 2008 Gary Teo
 * http://t.wits.sg
 * Copyright (c) 2008 Dominik Dzienia
 * http://www.dzienia.pl

USAGE:
	$(".someclass").progressBar();
	$("#progressbar").progressBar();
	$("#progressbar").progressBar(45);							// percentage
	$("#progressbar").progressBar({showText: false });			// percentage with config
	$("#progressbar").progressBar(45, {showText: false });		// percentage with config
*/
(function($) {
	$.extend({
		progressBar: new function(varunek) {
		
			this.defaults = {
				increment	: 2,
				speed		: 15,
				showText	: true,											// show text with percentage in next to the progressbar? - default : true
				width		: 120,											// Width of the progressbar - don't forget to adjust your image too!!!
				boxImage	: '../progressbar.gif',		// boxImage : image around the progress bar
				barImage	: '../progressbg_green.gif',	// Image to use in the progressbar. Can be an array of images too.
				height		: 12											// Height of the progressbar - don't forget to adjust your image too!!!
			};
			
			/* public methods */
			this.construct = function(arg1, arg2) {
				var argpercentage	= null;
				var argconfig		= null;
				
				if (arg1 != null) {
					if (!isNaN(arg1)) {
						argpercentage 	= arg1;
						if (arg2 != null) {
							argconfig	= arg2; }
					} else {
						argconfig		= arg1; 
					}
				}
				
				return this.each(function(child) {
					
					
					var adiv = $(this).get(0);
    				
					var confobj = jQuery.data(adiv, "config");
					if (confobj==null)
					{
						jQuery.data(adiv, "config", {cpercentage:0,tpercentage:0,builded:0});
						confobj = jQuery.data(adiv, "config");
					}
					
					var pb		= this;
					
					if ( ($('#'+pb.id+"_percentImage").attr('id') != '')&& jQuery.data(adiv, "config") != null && confobj.builded != 0) {
					
						
						confobj.tpercentage	= argpercentage;
						if (argconfig != null)
							confobj			= $.extend(confobj, argconfig);
							
						jQuery.data(adiv, "config", confobj);
							
					} else {
						
						var confobj				= $.extend(jQuery.data(adiv, "config"), $.progressBar.defaults, argconfig);
						var percentage			= argpercentage;
						
						confobj.builded = 1;
						
						if (argpercentage == null)
						{
							var percentage		= $(this).html().replace("%","");	// parsed percentage
						}	
						
						$(this).html(" ");
						
						$('<img>')
							.attr('id', pb.id + "_percentImage")
							.attr('src',confobj.boxImage)
							.attr('width', confobj.width)
							.css("width", confobj.width + "px")
							.css("height", confobj.height + "px")
							.css("background-image", "url(" + confobj.barImage + ")")
							.css("padding", "0")
							.css("margin", "0")
							.appendTo(this);
							

						$('<span>')
							.attr('id', pb.id + "_percentText")
							.appendTo(this);
						
						confobj.cpercentage	= 0;
						confobj.tpercentage	= percentage;
						
						jQuery.data(adiv, "config", confobj);
					}
					
					var t = setInterval(function() {
						
						var cpercentage = parseInt(confobj.cpercentage);
						var tpercentage = parseInt(confobj.tpercentage);
						var increment	= parseInt(confobj.increment);
						var bar			= $('#'+pb.id+"_percentImage");
						var text		= $('#'+pb.id+"_percentText")
						var pixels		= confobj.width / 100;			// Define how many pixels go into 1%
						
						bar.css("background-position", (((confobj.width * -1)) + (cpercentage * pixels)) + 'px 50%');
						
						if (confobj.showText)
							text.html(" " + Math.round(cpercentage) + "%");
						
						if (cpercentage > tpercentage) {
							if (cpercentage - increment  < tpercentage) {
								confobj.cpercentage = 0 + tpercentage
							} else {
								confobj.cpercentage -= increment;
							}
						}
						else if (confobj.cpercentage < confobj.tpercentage) {
							if (cpercentage + increment  > tpercentage) {
								confobj.cpercentage = tpercentage
							} else {
								confobj.cpercentage += increment;
							}
						} 
						else {
							clearInterval(t);
						}
						
						jQuery.data(adiv, "config", confobj);
					}, confobj.speed); 
				});
			};
		}
	});
		
	$.fn.extend({
        progressBar: $.progressBar.construct
	});
	
})(jQuery);